package com.hthy.futures.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.EOFException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
   * 
   * Author:  Mr.bingley
   * Version:
   * Date:  2017/5/19
   */

public class SerialbleTools {
     //序列化对象为String字符串，先对序列化后的结果进行BASE64编码，否则不能直接进行反序列化
     public static String writeObject(Object o) throws Exception {
         synchronized (SerialbleTools.class) {
             ByteArrayOutputStream bos = new ByteArrayOutputStream();
             ObjectOutputStream oos = new ObjectOutputStream(bos);
             oos.writeObject(o);
             oos.flush();
             oos.close();
             bos.close();
             return new String(bos.toByteArray(), "ISO-8859-1");
         }

     }

     //反序列化String字符串为对象

     public static Object readObject(String object) throws Exception{

         synchronized (SerialbleTools.class) {
             //ByteArrayInputStream bis = new ByteArrayInputStream(new BASE64Decoder().decodeBuffer(object));
             ByteArrayInputStream bis = new ByteArrayInputStream(object.getBytes("ISO-8859-1"));
             ObjectInputStream ois = new ObjectInputStream(bis);
             Object o = null;
             try {
                 o = ois.readObject();
             } catch (EOFException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
             }
             bis.close();
             ois.close();

             return o;
         }


     }

 }
